package com.itheima.provider.controller;

import com.itheima.provider.domain.Goods;
import com.itheima.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("goods")
//@DefaultProperties(defaultFallback = "fallBack")
public class GoodsController extends BaseController {
    @Autowired
    private GoodsService goodsService;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/find/{id}")
    @HystrixCommand(fallbackMethod = "fallBackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public Goods findById(@PathVariable("id") int id, String username) throws InterruptedException {
        //Thread.sleep(3000);
        if (id == 1) {
            int i = 1 / 0;
        }
        System.out.println(username);
        Goods goods = goodsService.findById(id);
        goods.setTitle(goods.getTitle() + port);
        return goods;
    }

    public Goods fallBackMethod(int id, String username) {
        Goods goods = new Goods();
        goods.setTitle("服务器正忙！");
        return goods;
    }
}
